$(function(){
	var approve_id;
	var data={};
	data.page=1;
	data.pagenum=10;
	var approvestate=1;
	var approvemain={}
	var where_json={};
		where_json.maintenance_record_id=['in',"{$Think.get.maintenance_record_id}"]
	data.where_json=JSON.stringify(where_json)
	
	ajaxserviceapprove()
	$('.back').on('click',function(){
		window.history.go(-1)
	})
	
	//请求审批维修记录信息
	function ajaxserviceapprove(){
		Load()
		$.ajax({
		type:'post',
		data:{
			where_json:JSON.stringify({maintenance_record_id:['in',"{$Think.get.maintenance_record_id}"]})
		},
		url:"{:url('adminapi/Maintenanceapprove/approveList')}",
		success:function(res){
			Load(1)
			if(res.code==1){
				console.log(res)
				if(res.list[0]){
					approvestate=res.list[0].approve_status
				}
				approvemain=res.list[0]
				ajaxservicedetail(data)
			}else{
				$("#loading-first").fadeOut(500);
				Err(res.msg)
			}
		},
		error:function(){
			Load(1)
			$("#loading-first").fadeOut(500);
			Err('请求失败')
		}
	})
	};
	//请求维修记录信息
	function ajaxservicedetail(data){
		$.ajax({
			type:'post',
			data:data,
			url:"{:url('adminapi/Maintenancerecord/recordList')}",
			success:function(res){
				$("#loading-first").fadeOut(500);
				console.log(res)
				console.log(approvestate)
				for(var key in res.list[0]){
					$('.'+key).html(res.list[0][key])
				}
				//审批已添加
				if(approvestate>=2){
					approve_id=approvemain.approve_id
					console.log('approve_id'+approve_id);
					console.log(approvemain);
					$('.check_main').css('display','block');
					$('.no_check_main').css('display','none');
					$('.check').attr('disabled',1);
					if(approvestate==2){
						$('.check').remove();
					}
					$('.checkedit').css('display','inline-block')
					if(approvestate==3){
						$('.check').html('审批通过');
						$('.check_ok').remove();
						$('.check_no').remove();
						$('.checkedit').css('display','none')
					}else if(approvestate==4){
						$('.check').html('审批不通过');
						$('.check_ok').remove();
						$('.check_no').remove();
						$('.checkedit').css('display','none')
					}
					
					
					for(var key in approvemain){
						$('.'+key).html(approvemain[key])
					}
					$('.material_cost_info_nochange tbody').html(cost_info_list(approvemain.material_cost_info))
					$('.working_hours_nochange tbody').html(work_hour_list(approvemain.working_hours))
					
					
				}else{
					//审批未添加
					
				};
				var checkdata={};
				checkdata.approve_id=approvemain.approve_id
				checkdata.maintenance_record_id=approvemain.maintenance_record_id
				checkdata.company_id=approvemain.company_id
				checkdata.maintenance_id=approvemain.maintenance_id
				checkdata.maintenance_time=approvemain.maintenance_time
				checkdata.contact_person=approvemain.contact_person
				checkdata.contact_number=approvemain.contact_number
				checkdata.kilometers=approvemain.kilometers
				checkdata.maintenance_desc=approvemain.maintenance_desc
				checkdata.remark=approvemain.remark
				checkdata.material_cost_info=approvemain.material_cost_info
				checkdata.working_hours=approvemain.working_hours
				checkdata.approve_person=approvemain.approve_person
				checkdata.approve_orgn=approvemain.approve_orgn
				checkdata.approve_status=approvemain.approve_status
				checkdata.approve_desc=approvemain.approve_desc
				checkdata.material_cost_info=JSON.stringify(approvemain.material_cost_info)
				checkdata.working_hours=JSON.stringify(approvemain.working_hours)
				$('.check_ok').on('click',function(){
					console.log(111)
					checkdata.approve_status=3;
					console.log(checkdata)
					Load()
					$.ajax({
						type:'post',
						data:checkdata,
						url:"{:url('adminapi/Maintenanceapprove/edit')}",
						success:function(res){
							Load(1)
							console.log(res)
							if(res.code==1){
								Msg('审批通过')
								window.history.go(0)
							}else{
								Err(res.msg)
							}
						},
						error:function(){
							Load(1)
							Err('请求失败')
						}
					})
				})
				$('.check_no').on('click',function(){
					checkdata.approve_status=4;
					$.ajax({
						type:'post',
						data:checkdata,
						url:"{:url('adminapi/Maintenanceapprove/edit')}",
						success:function(res){
							Load(1)
							if(res.code==1){
								Msg('审批不通过')
								window.history.go(0)
							}else{
								Err(res.msg)
							}
						},
						error:function(){
							Load(1)
							Err('请求失败')
						}
					})
				})
				
			},
			error:function(){
				$("#loading-first").fadeOut(500);
				Err('请求失败')
			}
		})
	}
	function cost_info_list(main){
		var costtbody="";
		for(var idx=0;idx<main.length;idx++){
			costtbody+=`<tr>`
			for(var key in main[idx]){
				costtbody+=`<td class='${key}'>${main[idx][key]}</td>`
			}
			costtbody+=`</tr>`
		}
		return costtbody
	}
	function work_hour_list(main){
		var worktbody="";
		for(var idx=0;idx<main.length;idx++){
			worktbody+=`<tr>`
			for(var key in main[idx]){
				worktbody+=`<td class='${key}'>${main[idx][key]}</td>`
			}
			worktbody+=`</tr>`
		}
		return worktbody
	}
})
